/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package reminder;

import java.net.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import configure.ConfigureInfo;
/**
 *
 * @author littlechild
 */
public class ReminderFrontActor {
    int port=Integer.parseInt(ConfigureInfo.getProperty(ConfigureInfo.ReminderPort));
    public ReminderObject[] getReminders(){
        try {
            Socket sock = new Socket("localhost", port);
            sock.getOutputStream().write(ReminderCommand.getReminders);
            sock.getOutputStream().flush();
            ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
            return (ReminderObject[]) in.readObject();
        } catch (IOException ex) {
            Logger.getLogger(ReminderFrontActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReminderFrontActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public boolean addReminder(ReminderObject obj) {
        try {
            Socket sock = new Socket("localhost", port);
            sock.getOutputStream().write(ReminderCommand.addReminder);
            sock.getOutputStream().flush();
            new ObjectOutputStream(sock.getOutputStream()).writeObject(obj);
            return true;
        } catch (UnknownHostException ex) {
            Logger.getLogger(ReminderFrontActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ReminderFrontActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public boolean deleteReminder(int index) {
        try {
            Socket sock = new Socket("localhost", port);
            sock.getOutputStream().write(ReminderCommand.deleteReminder);
            sock.getOutputStream().flush();
            DataOutputStream dout=new DataOutputStream(sock.getOutputStream());
            dout.writeInt(index);
            return true;
        } catch (UnknownHostException ex) {
            Logger.getLogger(ReminderFrontActor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ReminderFrontActor.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
